/* 
 * Promise 封装 api
 */
 
/* 
 * uni.getSystemInfo
 */
const getSystemInfo = () => {   
	return new Promise((resolve, reject) => {
		uni.getSystemInfo({ 
			success: (res) => { 
				resolve(res);		
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

/*
 * opts:请求参数 url, 
 * 网络资源保存临时地址
 */
const downloadFile = (opts) => { 
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url:opts.url,			 
			success: (res) => { 
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

/*
 * opts:请求参数 url,data,datatype,method, 
 * canvas绘制导出
 */
const canvasToTempFilePath = (opts) => {   
	return new Promise((resolve, reject) => {
		uni.canvasToTempFilePath({
			x:''||opts.x,
			y: ''||opts.y,
			width: ''||opts.width,
			height: ''||opts.height,
			destWidth: ''||opts.destWidth,
			destHeight: ''||opts.destHeight,
			quality:''||opts.destHeight,
			fileType:''||opts.fileType,
			canvasId: opts.canvasId,			 
			success: (res) => { 
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

const uniShowToast = (opts) => { 
	let title=opts.title,
		icon='',
		image='',
		duration=2000;
	if(opts.icon&&(opts.icon=='success'||opts.icon=='loading')){
		image='';
		icon=opts.icon;
	}else{
		image=opts.image;
		icon='none';
	} 
	if(opts.duration){
		duration=opts.duration;
	}
	return new Promise((resolve, reject) => {
		uni.showToast({ 
			title:title,
			icon:icon,
			image:image,
			mask:true,
			duration:duration,
			success: (res) => { 
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

const uniShowLoading = (title) => { 
	// let title=opts.title; 	 
	return new Promise((resolve, reject) => {
		uni.showLoading({ 
			title:title,  
			mask:true, 
			success: (res) => { 
				resolve(res);
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

/* 
 *  上传文件
 *  type=1  自定义接口
 */
const uniUploadFile = async (options) => { 	 
	let url='';
	if(options.type&&options.type==1){
		url=options.url;
	}else{
		url=HTTP_REQUEST_URL+options.url;
	}
	return new Promise((resolve, reject) => {		 
		uni.uploadFile({
			url: url,
			filePath: options.filePath,
			name: options.name,
			header: {
				"Content-Type": "multipart/form-data",
				'Cookie': uni.getStorageSync('cookiekey')
			},
			formData: options.formData,
			success(res) {  
				resolve(res); 
			},
			fail() {
				reject();
			}
		});	 
	});
};

 

export {
	getSystemInfo,
	downloadFile,
	canvasToTempFilePath,
	uniShowToast,
	uniShowLoading,
	uniUploadFile
};